package org.lee;

import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lizhe
 */
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbc;

    @Override
    public void run(String... args) throws Exception {

        List<Student> list = queryList();
        print(list);

        List<Long> ids = list.stream().map(Student::getId).collect(Collectors.toList());
        delete(ids);

        List<Student> students = queryById(Joiner.on(",").join(ids));
        print(students);

    }

    private void delete(List<Long> ids) {

        List<Map<String, Long>> params = new LinkedList<>();
        for (Long id : ids) {
            HashMap<String, Long> param = new HashMap<>();
            param.put("id", id);
            params.add(param);
        }

        Map<String, String>[] ar = params.toArray(new HashMap[params.size()]);
        int[] ints = namedParameterJdbc.batchUpdate("delete from student where id = :id", ar);
        System.out.println(ints.toString());
    }

    private List<Student> queryById(String idStr) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("ids", idStr);
        List<Student> list = namedParameterJdbc.query("select * from student where id in (:ids)", paramMap, (rs, rowNum) -> {
            Student bean = new Student();
            bean.setId(rs.getLong("id"));
            bean.setName(rs.getString("name"));
            bean.setCourse(rs.getString("course"));
            bean.setGender(rs.getString("gender"));
            bean.setAddTime(rs.getString("addTime"));
            return bean;
        });

        return list;
    }

    private void print(List<Student> list) {
        if (list.size() == 0) {
            System.out.println("没有查到数据");
        }
        for (Student student : list) {
            System.out.println(student.toString());
        }
    }

    private List<Student> queryList() {
        List<Student> list = namedParameterJdbc.query("select * from student limit 10   ", (rs, rowNum) -> {
            Student bean = new Student();
            bean.setId(rs.getLong("id"));
            bean.setName(rs.getString("name"));
            bean.setCourse(rs.getString("course"));
            bean.setGender(rs.getString("gender"));
            bean.setAddTime(rs.getString("addTime"));
            return bean;
        });

        return list;
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
